package xyz.bali16.module.blog.service.impl;

/**
 * @author bali2 on 7/11/2021.
 * @version 1.0
 */

import com.gitee.starblues.bootstrap.annotation.AutowiredType;
import io.swagger.annotations.ApiOperation;
import lombok.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.application.core.utils.AuthUtils;
import xyz.bali16.application.entity.User;
import xyz.bali16.application.service.UserService;
import xyz.bali16.module.blog.utils.PageUtils;
import xyz.bali16.module.netdisk.entity.FileUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 用来装载调用网盘者的相关信息
 */


@Getter
@Setter
@Service
@NoArgsConstructor
public class NetdiskArticleCallService {
    @Autowired
    NetdiskServiceCaller netdiskServiceCaller;

    @Autowired
    @AutowiredType(AutowiredType.Type.PLUGIN_MAIN)
    private UserService userService;
    @Autowired
    private FileUserServiceCaller fileUserServiceCaller;

    //@Autowired
    //private FileFolderService fileFolderService;
    //@Autowired
    //private FileStorageService fileStorageService;
    private String fileUserId = "80";
    private String fileStorageId = "80";
    private String fileFolderId = "80";

    @ApiOperation("以个人身份上传到博客文章文件仓库中")
    public Result<String> myUpload(MultipartFile file, HttpServletRequest request) {
        String username = AuthUtils.getCurrentUsername();
        User user = userService.getByUsername(username);
        FileUser fileUser = fileUserServiceCaller.getFileUserBySysUserId(user.getUserId());
         // 需要做是否存在fileUser检验
        return netdiskServiceCaller.uploadFile(file, fileUser.getId(), fileStorageId, fileFolderId, request);
    }

    public Result<String> upload(MultipartFile file, HttpServletRequest request) {
        return netdiskServiceCaller.uploadFile(file, fileUserId, fileStorageId, fileFolderId, request);
    }

    public void download(String fileId, HttpServletRequest request, HttpServletResponse response) {
        netdiskServiceCaller.downloadFile(fileUserId, fileStorageId, fileId, request, response);
    }

    public PageUtils list(Integer curPage, Integer pageSize) {
        return netdiskServiceCaller.list(fileStorageId, fileFolderId, curPage, pageSize);
    }

    public boolean delete(String fileId) {
        return netdiskServiceCaller.deleteFile(fileId);
    }

}
